package com.cloudcross.ssp.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloudcross.ssp.base.dao.GenericIBatisDao;
import com.cloudcross.ssp.common.utils.MapBuilder;
import com.cloudcross.ssp.common.utils.Pager;
import com.cloudcross.ssp.common.utils.SqlHelper;
import com.cloudcross.ssp.model.Operator;
import com.cloudcross.ssp.service.IOperatorService;
import com.cloudcross.ssp.web.widget.SelectorController.ISelector;
import com.cloudcross.ssp.web.widget.SelectorController.ISelectorProvider;

@Service
public class OperatorService implements IOperatorService, ISelectorProvider {

    @Autowired
    protected GenericIBatisDao myBatisDao;

    @Override
    public Operator findById(Long id) {
        return myBatisDao.get("operatorSqlMapper.findById", id);
    }

    @Override
    public int countByParams(Map<String, Object> paramMap) {
        return myBatisDao.get("operatorSqlMapper.countByParams", paramMap);
    }

    @Override
    public int countByParamsBack(Map<String, Object> paramMap) {
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("value", searchValue);
        return myBatisDao.get("operatorSqlMapper.countByParams", paramMap);
    }

    @Override
    public List<Operator> findByParams(Map<String, Object> paramMap, Pager pager) {
        paramMap.put("paramMap", paramMap);
        return myBatisDao.getList("operatorSqlMapper.findByParams", paramMap);
    }

    @Override
    public List<Operator> findByParamsBack(Map<String, Object> paramMap, Pager pager) {
        paramMap.put("pager", pager);
        String searchValue = SqlHelper.doLike(paramMap.get("searchValue"));
        paramMap.put("value", searchValue);
        return myBatisDao.getList("operatorSqlMapper.findByParams", paramMap);
    }

    @Override
    public boolean add(Operator t) {
        myBatisDao.save("operatorSqlMapper.addOperator", t);
        return true;
    }

    @Override
    public boolean edit(Operator t) {
        myBatisDao.save("operatorSqlMapper.editOperator", t);
        return true;
    }

    @Override
    public boolean updateStatus(List<Long> idList, int status) {
        return false;
    }

    @Override
    public boolean updateStatusBack(List<Long> idList, int status) {
        myBatisDao.save("operatorSqlMapper.updateOperatorStatus",
                MapBuilder.create(HashMap.class).add("operatorIdList", idList).add("modifyDate", new Date()).add("status", status).map());
        return true;
    }

    public List<Operator> findAllOperator() {
        return myBatisDao.getList("operatorSqlMapper.findAllOperator");
    }

    @Override
    public Map<String, List<? extends ISelector>> provideData(String[] parentId) {
        List<Operator> operatorList = findAllOperator();
        return MapBuilder.create(HashMap.class).add("operatorList", operatorList).map();
    }

    @Override
    public Operator getNew(Operator o) {
        return myBatisDao.get("operatorSqlMapper.getNew", o);
    }

    public List<Operator> queryAllOperator(Map<String, Object> condition) {
        return myBatisDao.getList("operatorSqlMapper.queryAll", condition);
    }

    public Integer queryAllOperatorCount() {
        return myBatisDao.get("operatorSqlMapper.queryAllCount", null);
    }
}
